#pragma execution_character_set("utf-8")
#include "devicesetup.h"
#include "ui_devicesetup.h"
#include "messagebox.h"
#include "stoast.h"
#include "myhelper.h"

DeviceSetup::DeviceSetup(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DeviceSetup)
{
    ui->setupUi(this);

    netDevNameList << ui->label_netName_1 << ui->label_netName_2 << ui->label_netName_3 << ui->label_netName_4
                   << ui->label_netName_5 << ui->label_netName_6 << ui->label_netName_7 << ui->label_netName_8;
    macRawNameList << ui->label_mac_raw_1 << ui->label_mac_raw_2 << ui->label_mac_raw_3 << ui->label_mac_raw_4
                   << ui->label_mac_raw_5 << ui->label_mac_raw_6 << ui->label_mac_raw_7 << ui->label_mac_raw_8;
    macNewNameList << ui->label_mac_new_1 << ui->label_mac_new_2 << ui->label_mac_new_3 << ui->label_mac_new_4
                   << ui->label_mac_new_5 << ui->label_mac_new_6 << ui->label_mac_new_7 << ui->label_mac_new_8;

    ui->comboBox_devType->setCurrentIndex(ui->comboBox_devType->count()-1);
    ui->spinBox_mac_5->setValue(9);
    ui->spinBox_mac_6->setValue(100);
}

void DeviceSetup::SetDeviceInfo(UdpDevice *udevice, int dev_index)
{
    netDevNums = udevice->macStr.length()/17;

    for(int i=0; i<netDevNums; i++){
        netDevNameList[i]->setText(QString("网卡%1").arg(i+1));
        macRawNameList[i]->setText(udevice->macStr.mid(i*17, 17));
        macNewNameList[i]->setText("--:--:--:--:--:--");
    }
    for(int i=netDevNums; i<8; i++){
        netDevNameList[i]->setText("---");
        macRawNameList[i]->setText("--:--:--:--:--:--");
        macNewNameList[i]->setText("--:--:--:--:--:--");
    }

    devIndex        = dev_index;
    ui->textBrowser->clear();
    ui->textBrowser->append(udevice->testLogs);
    // ui->textBrowser->moveCursor(QTextCursor::End);
}

void DeviceSetup::appendInfo(QString info)
{
    ui->textBrowser->append(info);
}

DeviceSetup::~DeviceSetup()
{
    delete ui;
}

void DeviceSetup::on_pushButton_refreshMAC_clicked()
{
    macAddress[0] = ui->spinBox_mac_1->value();
    macAddress[1] = ui->spinBox_mac_2->value();
    macAddress[2] = ui->spinBox_mac_3->value();
    macAddress[3] = ui->spinBox_mac_4->value();
    macAddress[4] = ui->spinBox_mac_5->value();
    macAddress[5] = ui->spinBox_mac_6->value();

    if(macAddress[5] + netDevNums > 256){
        Message::showWarning(this, "警告", QString("MAC地址的第六位需要<%1").arg(QString::number(256-netDevNums, 16)));
        return;
    }

    for(int i=0; i<netDevNums; i++){
        macNewNameList[i]->setText(QString("%1:%2:%3:%4:%5:%6")
                .arg(QString::number(macAddress[0],16))
                .arg(QString::number(macAddress[1],16))
                .arg(QString::number(macAddress[2],16))
                .arg(QString::number(macAddress[3],16))
                .arg(QString::number(macAddress[4],16))
                .arg(QString::number(macAddress[5]+i, 16)));
    }
    for(int i=netDevNums; i<8; i++){
        macNewNameList[i]->setText("--:--:--:--:--:--");
    }
}

void DeviceSetup::on_pushButton_setMAC_clicked()
{
    int offset = 0;
    macAddress[0] = ui->spinBox_mac_1->value();
    macAddress[1] = ui->spinBox_mac_2->value();
    macAddress[2] = ui->spinBox_mac_3->value();
    macAddress[3] = ui->spinBox_mac_4->value();
    macAddress[4] = ui->spinBox_mac_5->value();
    macAddress[5] = ui->spinBox_mac_6->value();

    if(isStartTestCase){
        Message::showWarning(NULL, "警告", "请先停止老化测试，再进行设备设置！ ");
        return;
    }

    int devTypeIndex = ui->comboBox_devType->currentIndex();

    QString devTypeStr = "COME-B板 ";
    if(netDevNums <= 2 && devTypeIndex == 0){
        devTypeStr = "COME-B板 ";
    }else if(netDevNums == 8 && devTypeIndex == 0){
        devTypeStr = "网安板 ";
    }else if(netDevNums == 8 && devTypeIndex == 1){
        devTypeStr = "新版网安板 ";
    }else if(netDevNums <= 2 && devTypeIndex == 2){
        devTypeStr = "新版COME-B(I210网卡) ";
    }else if(netDevNums == 1 && devTypeIndex == 3){
        devTypeStr = "视新板（I210网卡）";
    }else if(netDevNums == 2 && devTypeIndex == 5){
        devTypeStr = "十六核网安板";
    }else if(devTypeIndex == 6){
        devTypeStr = "FT2004新四核CPU网卡";
    }else {
        Message::showWarning(this, "警告", QString("检测到未知的设备，当前主板网卡数量为%1").arg(netDevNums));
        return;
    }
    QString infoStr = QString("确定设置MAC地址吗？，当前版本类型为%1(MAC地址更改完重启后生效)").arg(devTypeStr);
    if(!myHelper::ShowMessageBoxQuesion(infoStr)){
        SingleTonSToast::toast("操作已取消 ");
        return;
    }

    ui->textBrowser->append(QString("开始对‘%1’进行MAC地址配置，当前网卡数量为%2").arg(devTypeStr).arg(netDevNums));
    if(devTypeIndex == 6){
        // FT2004新四核CPU网卡
        QString cmdStr = QString("i2cset -y -f 0 0x57 0xa0 %1 2>/dev/null;sleep 0.2;"
                                 "i2cset -y -f 0 0x57 0xa1 %2 2>/dev/null;sleep 0.2;"
                                 "i2cset -y -f 0 0x57 0xa2 %3 2>/dev/null;sleep 0.2;")
                                    .arg(macAddress[0])
                                    .arg(macAddress[1])
                                    .arg(macAddress[2]);
        emit startOneOrder_udp(devIndex, cmdStr, 10);
                cmdStr = QString("i2cset -y -f 0 0x57 0xa3 %4 2>/dev/null;sleep 0.2;"
                                 "i2cset -y -f 0 0x57 0xa4 %5 2>/dev/null;sleep 0.2;"
                                 "i2cset -y -f 0 0x57 0xa5 %6 2>/dev/null;sleep 0.1;"
                                 "i2cdump -y -f -r 0xa0-0xa5 0 0x57 b;")
                                    .arg(macAddress[3])
                                    .arg(macAddress[4])
                                    .arg(macAddress[5]);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

                cmdStr = QString("i2cset -y -f 0 0x57 0xa6 %1 2>/dev/null;sleep 0.2;"
                                 "i2cset -y -f 0 0x57 0xa7 %2 2>/dev/null;sleep 0.2;"
                                 "i2cset -y -f 0 0x57 0xa8 %3 2>/dev/null;sleep 0.2;")
                                    .arg(macAddress[0])
                                    .arg(macAddress[1])
                                    .arg(macAddress[2]);
        emit startOneOrder_udp(devIndex, cmdStr, 10);
                cmdStr = QString("i2cset -y -f 0 0x57 0xa9 %4 2>/dev/null;sleep 0.2;"
                                 "i2cset -y -f 0 0x57 0xaa %5 2>/dev/null;sleep 0.2;"
                                 "i2cset -y -f 0 0x57 0xab %6 2>/dev/null;sleep 0.1;"
                                 "i2cdump -y -f -r 0xa0-0xab 0 0x57 b;")
                                    .arg(macAddress[3])
                                    .arg(macAddress[4])
                                    .arg(macAddress[5] + 1);
        emit startOneOrder_udp(devIndex, cmdStr, 10);
        return;
    }

    if(netDevNums <= 2){
        if(devTypeIndex == 5){
            /* 十六核网安板，修改两个网卡的MAC地址 */
            QString magicStr = "0x15338086";
            QString cmdStr = QString("ethtool -E enp%5s0 magic %4 offset 0x00 value %1;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x01 value %2;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x02 value %3;")
                    .arg(macAddress[0])
                    .arg(macAddress[1])
                    .arg(macAddress[2])
                    .arg(magicStr)
                    .arg(10);

            emit startOneOrder_udp(devIndex, cmdStr, 10);

            cmdStr =         QString("ethtool -E enp%5s0 magic %4 offset 0x03 value %1;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x04 value %2;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x05 value %3;"
                                     "ethtool -e enp%5s0 offset 0 length 6")
                    .arg(macAddress[3])
                    .arg(macAddress[4])
                    .arg(macAddress[5])
                    .arg(magicStr)
                    .arg(10);
            emit startOneOrder_udp(devIndex, cmdStr, 10);

            cmdStr = QString("ethtool -E enp%5s0 magic %4 offset 0x00 value %1;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x01 value %2;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x02 value %3;")
                    .arg(macAddress[0])
                    .arg(macAddress[1])
                    .arg(macAddress[2])
                    .arg(magicStr)
                    .arg(11);

            emit startOneOrder_udp(devIndex, cmdStr, 10);

            cmdStr =         QString("ethtool -E enp%5s0 magic %4 offset 0x03 value %1;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x04 value %2;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x05 value %3;"
                                     "ethtool -e enp%5s0 offset 0 length 6")
                    .arg(macAddress[3])
                    .arg(macAddress[4])
                    .arg(macAddress[5] + 1)
                    .arg(magicStr)
                    .arg(11);
            emit startOneOrder_udp(devIndex, cmdStr, 10);
        }else{
            /* 修改一个网卡的MAC地址 */
            QString magicStr = "0x10d38086";
            if(devTypeIndex == 2 || devTypeIndex == 3){         // 新版COME-B，或者视新板 采用I210网卡
                magicStr = "0x15338086";
            }
            QString cmdStr = QString("ethtool -E enp%5s0 magic %4 offset 0x00 value %1;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x01 value %2;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x02 value %3;")
                    .arg(macAddress[0])
                    .arg(macAddress[1])
                    .arg(macAddress[2])
                    .arg(magicStr)
                    .arg(devTypeIndex==3?7:10);

            emit startOneOrder_udp(devIndex, cmdStr, 10);

            cmdStr =         QString("ethtool -E enp%5s0 magic %4 offset 0x03 value %1;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x04 value %2;"
                                     "ethtool -E enp%5s0 magic %4 offset 0x05 value %3;"
                                     "ethtool -e enp%5s0 offset 0 length 6")
                    .arg(macAddress[3])
                    .arg(macAddress[4])
                    .arg(macAddress[5])
                    .arg(magicStr)
                    .arg(devTypeIndex==3?7:10);
            emit startOneOrder_udp(devIndex, cmdStr, 10);
        }
    }else if(netDevNums == 8 && devTypeIndex == 0){

        SingleTonSToast::toast("设置中，请等待...");
        /* 修改网安板8个网卡的MAC地址 */
        /* LED1 初始化 */
        QString cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset 0x3e value 0x42;"
                                 "ethtool -E enp3s0f0 magic 0x15218086 offset 0x38 value 0x84;"
                                 "ethtool -e enp3s0f0 offset 0x20 length 32;");
        emit startOneOrder_udp(devIndex, cmdStr, 10);
        /* LED2 初始化 */
                cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset 0x13e value 0x42;"
                                 "ethtool -E enp3s0f0 magic 0x15218086 offset 0x138 value 0x84;"
                                 "ethtool -e enp3s0f0 offset 0x120 length 32;");
        emit startOneOrder_udp(devIndex, cmdStr, 10);
        /* LED3 初始化 */
                cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset 0x1be value 0x42;"
                                 "ethtool -E enp3s0f0 magic 0x15218086 offset 0x1b8 value 0x84;"
                                 "ethtool -e enp3s0f0 offset 0x1a0 length 32;");
        emit startOneOrder_udp(devIndex, cmdStr, 10);
        /* LED4 初始化 */
                cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset 0x23e value 0x42;"
                                 "ethtool -E enp3s0f0 magic 0x15218086 offset 0x238 value 0x84;"
                                 "ethtool -e enp3s0f0 offset 0x220 length 32;");
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I210 网卡1 MAC配置 */
        cmdStr = QString("ethtool -E enp8s0 magic 0x15338086 offset 0x00 value %1;"
                         "ethtool -E enp8s0 magic 0x15338086 offset 0x01 value %2;"
                         "ethtool -E enp8s0 magic 0x15338086 offset 0x02 value %3;")
        .arg(macAddress[0])
        .arg(macAddress[1])
        .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp8s0 magic 0x15338086 offset 0x03 value %1;"
                         "ethtool -E enp8s0 magic 0x15338086 offset 0x04 value %2;"
                         "ethtool -E enp8s0 magic 0x15338086 offset 0x05 value %3;"
                         "ethtool -e enp8s0 offset 0 length 6")
        .arg(macAddress[3])
        .arg(macAddress[4])
        .arg(macAddress[5]);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I210 网卡2 MAC配置 */
        cmdStr = QString("ethtool -E enp12s0 magic 0x15338086 offset 0x00 value %1;"
                         "ethtool -E enp12s0 magic 0x15338086 offset 0x01 value %2;"
                         "ethtool -E enp12s0 magic 0x15338086 offset 0x02 value %3;")
        .arg(macAddress[0])
        .arg(macAddress[1])
        .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp12s0 magic 0x15338086 offset 0x03 value %1;"
                         "ethtool -E enp12s0 magic 0x15338086 offset 0x04 value %2;"
                         "ethtool -E enp12s0 magic 0x15338086 offset 0x05 value %3;"
                         "ethtool -e enp12s0 offset 0 length 6")
        .arg(macAddress[3])
        .arg(macAddress[4])
        .arg(macAddress[5]+1);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I210 网卡3 MAC配置 */
        cmdStr = QString("ethtool -E enp13s0 magic 0x15338086 offset 0x00 value %1;"
                         "ethtool -E enp13s0 magic 0x15338086 offset 0x01 value %2;"
                         "ethtool -E enp13s0 magic 0x15338086 offset 0x02 value %3;")
        .arg(macAddress[0])
        .arg(macAddress[1])
        .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp13s0 magic 0x15338086 offset 0x03 value %2;"
                         "ethtool -E enp13s0 magic 0x15338086 offset 0x04 value %3;"
                         "ethtool -E enp13s0 magic 0x15338086 offset 0x05 value %4;"
                         "ethtool -e enp13s0 offset 0 length 6")
        .arg(macAddress[3])
        .arg(macAddress[4])
        .arg(macAddress[5]+2);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I210 网卡4 MAC配置 */
        cmdStr = QString("ethtool -E enp14s0 magic 0x15338086 offset 0x00 value %1;"
                         "ethtool -E enp14s0 magic 0x15338086 offset 0x01 value %2;"
                         "ethtool -E enp14s0 magic 0x15338086 offset 0x02 value %3;")
        .arg(macAddress[0])
        .arg(macAddress[1])
        .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp14s0 magic 0x15338086 offset 0x03 value %1;"
                         "ethtool -E enp14s0 magic 0x15338086 offset 0x04 value %2;"
                         "ethtool -E enp14s0 magic 0x15338086 offset 0x05 value %3;"
                         "ethtool -e enp14s0 offset 0 length 6")
        .arg(macAddress[3])
        .arg(macAddress[4])
        .arg(macAddress[5]+3);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I350 网口1 MAC配置 */
        offset = 0;
        cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %5 value %6;")
                .arg(offset)
                .arg(macAddress[0])
                .arg(offset+1)
                .arg(macAddress[1])
                .arg(offset+2)
                .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %5 value %6;"
                         "ethtool -e enp3s0f0 offset %7 length 6")
                .arg(offset+3)
                .arg(macAddress[3])
                .arg(offset+4)
                .arg(macAddress[4])
                .arg(offset+5)
                .arg(macAddress[5]+4)
                .arg(offset);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I350 网口2 MAC配置 */
        offset = 256;
        cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %5 value %6;")
                .arg(offset)
                .arg(macAddress[0])
                .arg(offset+1)
                .arg(macAddress[1])
                .arg(offset+2)
                .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %5 value %6;"
                         "ethtool -e enp3s0f0 offset %7 length 6")
                .arg(offset+3)
                .arg(macAddress[3])
                .arg(offset+4)
                .arg(macAddress[4])
                .arg(offset+5)
                .arg(macAddress[5]+5)
                .arg(offset);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I350 网口3 MAC配置 */
        offset = 384;
        cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %5 value %6;")
                .arg(offset)
                .arg(macAddress[0])
                .arg(offset+1)
                .arg(macAddress[1])
                .arg(offset+2)
                .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %5 value %6;"
                         "ethtool -e enp3s0f0 offset %7 length 6")
                .arg(offset+3)
                .arg(macAddress[3])
                .arg(offset+4)
                .arg(macAddress[4])
                .arg(offset+5)
                .arg(macAddress[5]+6)
                .arg(offset);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I350 网口4 MAC配置 */
        offset = 512;
        cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %5 value %6;")
                .arg(offset)
                .arg(macAddress[0])
                .arg(offset+1)
                .arg(macAddress[1])
                .arg(offset+2)
                .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp3s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp3s0f0 magic 0x15218086 offset %5 value %6;"
                         "ethtool -e enp3s0f0 offset %7 length 6")
                .arg(offset+3)
                .arg(macAddress[3])
                .arg(offset+4)
                .arg(macAddress[4])
                .arg(offset+5)
                .arg(macAddress[5]+7)
                .arg(offset);
        emit startOneOrder_udp(devIndex, cmdStr, 10);
    }else if(netDevNums == 8 && devTypeIndex == 1){

        SingleTonSToast::toast("设置中，请等待...");
        /* 修改网安板8个网卡的MAC地址 */
        /* LED1 初始化 */
        QString cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset 0x3e value 0x42;"
                                 "ethtool -E enp10s0f0 magic 0x15218086 offset 0x38 value 0x84;"
                                 "ethtool -e enp10s0f0 offset 0x20 length 32;");
        emit startOneOrder_udp(devIndex, cmdStr, 10);
        /* LED2 初始化 */
                cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset 0x13e value 0x42;"
                                 "ethtool -E enp10s0f0 magic 0x15218086 offset 0x138 value 0x84;"
                                 "ethtool -e enp10s0f0 offset 0x120 length 32;");
        emit startOneOrder_udp(devIndex, cmdStr, 10);
        /* LED3 初始化 */
                cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset 0x1be value 0x42;"
                                 "ethtool -E enp10s0f0 magic 0x15218086 offset 0x1b8 value 0x84;"
                                 "ethtool -e enp10s0f0 offset 0x1a0 length 32;");
        emit startOneOrder_udp(devIndex, cmdStr, 10);
        /* LED4 初始化 */
                cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset 0x23e value 0x42;"
                                 "ethtool -E enp10s0f0 magic 0x15218086 offset 0x238 value 0x84;"
                                 "ethtool -e enp10s0f0 offset 0x220 length 32;");
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I210 网卡1 MAC配置 */
        cmdStr = QString("ethtool -E enp11s0 magic 0x15338086 offset 0x00 value %1;"
                         "ethtool -E enp11s0 magic 0x15338086 offset 0x01 value %2;"
                         "ethtool -E enp11s0 magic 0x15338086 offset 0x02 value %3;")
        .arg(macAddress[0])
        .arg(macAddress[1])
        .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp11s0 magic 0x15338086 offset 0x03 value %1;"
                         "ethtool -E enp11s0 magic 0x15338086 offset 0x04 value %2;"
                         "ethtool -E enp11s0 magic 0x15338086 offset 0x05 value %3;"
                         "ethtool -e enp11s0 offset 0 length 6")
        .arg(macAddress[3])
        .arg(macAddress[4])
        .arg(macAddress[5]);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I210 网卡2 MAC配置 */
        cmdStr = QString("ethtool -E enp12s0 magic 0x15338086 offset 0x00 value %1;"
                         "ethtool -E enp12s0 magic 0x15338086 offset 0x01 value %2;"
                         "ethtool -E enp12s0 magic 0x15338086 offset 0x02 value %3;")
        .arg(macAddress[0])
        .arg(macAddress[1])
        .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp12s0 magic 0x15338086 offset 0x03 value %1;"
                         "ethtool -E enp12s0 magic 0x15338086 offset 0x04 value %2;"
                         "ethtool -E enp12s0 magic 0x15338086 offset 0x05 value %3;"
                         "ethtool -e enp12s0 offset 0 length 6")
        .arg(macAddress[3])
        .arg(macAddress[4])
        .arg(macAddress[5]+1);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I210 网卡3 MAC配置 */
        cmdStr = QString("ethtool -E enp13s0 magic 0x15338086 offset 0x00 value %1;"
                         "ethtool -E enp13s0 magic 0x15338086 offset 0x01 value %2;"
                         "ethtool -E enp13s0 magic 0x15338086 offset 0x02 value %3;")
        .arg(macAddress[0])
        .arg(macAddress[1])
        .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp13s0 magic 0x15338086 offset 0x03 value %2;"
                         "ethtool -E enp13s0 magic 0x15338086 offset 0x04 value %3;"
                         "ethtool -E enp13s0 magic 0x15338086 offset 0x05 value %4;"
                         "ethtool -e enp13s0 offset 0 length 6")
        .arg(macAddress[3])
        .arg(macAddress[4])
        .arg(macAddress[5]+2);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I210 网卡4 MAC配置 */
        cmdStr = QString("ethtool -E enp14s0 magic 0x15338086 offset 0x00 value %1;"
                         "ethtool -E enp14s0 magic 0x15338086 offset 0x01 value %2;"
                         "ethtool -E enp14s0 magic 0x15338086 offset 0x02 value %3;")
        .arg(macAddress[0])
        .arg(macAddress[1])
        .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp14s0 magic 0x15338086 offset 0x03 value %1;"
                         "ethtool -E enp14s0 magic 0x15338086 offset 0x04 value %2;"
                         "ethtool -E enp14s0 magic 0x15338086 offset 0x05 value %3;"
                         "ethtool -e enp14s0 offset 0 length 6")
        .arg(macAddress[3])
        .arg(macAddress[4])
        .arg(macAddress[5]+3);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I350 网口1 MAC配置 */
        offset = 0;
        cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %5 value %6;")
                .arg(offset)
                .arg(macAddress[0])
                .arg(offset+1)
                .arg(macAddress[1])
                .arg(offset+2)
                .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %5 value %6;"
                         "ethtool -e enp10s0f0 offset %7 length 6")
                .arg(offset+3)
                .arg(macAddress[3])
                .arg(offset+4)
                .arg(macAddress[4])
                .arg(offset+5)
                .arg(macAddress[5]+4)
                .arg(offset);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I350 网口2 MAC配置 */
        offset = 256;
        cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %5 value %6;")
                .arg(offset)
                .arg(macAddress[0])
                .arg(offset+1)
                .arg(macAddress[1])
                .arg(offset+2)
                .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %5 value %6;"
                         "ethtool -e enp10s0f0 offset %7 length 6")
                .arg(offset+3)
                .arg(macAddress[3])
                .arg(offset+4)
                .arg(macAddress[4])
                .arg(offset+5)
                .arg(macAddress[5]+5)
                .arg(offset);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I350 网口3 MAC配置 */
        offset = 384;
        cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %5 value %6;")
                .arg(offset)
                .arg(macAddress[0])
                .arg(offset+1)
                .arg(macAddress[1])
                .arg(offset+2)
                .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %5 value %6;"
                         "ethtool -e enp10s0f0 offset %7 length 6")
                .arg(offset+3)
                .arg(macAddress[3])
                .arg(offset+4)
                .arg(macAddress[4])
                .arg(offset+5)
                .arg(macAddress[5]+6)
                .arg(offset);
        emit startOneOrder_udp(devIndex, cmdStr, 10);

        SingleTonSToast::msleep(500);
        /* I350 网口4 MAC配置 */
        offset = 512;
        cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %5 value %6;")
                .arg(offset)
                .arg(macAddress[0])
                .arg(offset+1)
                .arg(macAddress[1])
                .arg(offset+2)
                .arg(macAddress[2]);

        emit startOneOrder_udp(devIndex, cmdStr, 10);

        cmdStr = QString("ethtool -E enp10s0f0 magic 0x15218086 offset %1 value %2;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %3 value %4;"
                         "ethtool -E enp10s0f0 magic 0x15218086 offset %5 value %6;"
                         "ethtool -e enp10s0f0 offset %7 length 6")
                .arg(offset+3)
                .arg(macAddress[3])
                .arg(offset+4)
                .arg(macAddress[4])
                .arg(offset+5)
                .arg(macAddress[5]+7)
                .arg(offset);
        emit startOneOrder_udp(devIndex, cmdStr, 10);
    }
}

void DeviceSetup::on_pushButton_clear_clicked()
{
    ui->textBrowser->clear();
}

void DeviceSetup::on_pushButton_readhwrtc_clicked()
{
    if(isStartTestCase){
        Message::showWarning(NULL, "警告", "请先停止老化测试，再进行设备设置！ ");
        return;
    }
    QString cmdStr = QString("hwclock -r");
    emit startOneOrder_udp(devIndex, cmdStr, 10);
}

void DeviceSetup::on_pushButton_sethwrtc_clicked()
{
    if(isStartTestCase){
        Message::showWarning(NULL, "警告", "请先停止老化测试，再进行设备设置！ ");
        return;
    }
    QString datestr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString infoStr = QString("确定设置测试板-%1的实时时钟为：%2 吗？").arg(devIndex+1).arg(datestr);
    if(!myHelper::ShowMessageBoxQuesion(infoStr)){
        SingleTonSToast::toast("操作已取消 ");
        return;
    }
    QString cmdStr = QString("date -s \"%1\" ; sync ; sleep 1; hwclock -w").arg(datestr);
    emit startOneOrder_udp(devIndex, cmdStr, 20);

    SingleTonSToast::toast("时钟设置中，请等待...");
}

void DeviceSetup::on_pushButton_setMAC_baseBoard_clicked()
{
    int offset = 0;
    macAddress[0] = ui->spinBox_mac_1->value();
    macAddress[1] = ui->spinBox_mac_2->value();
    macAddress[2] = ui->spinBox_mac_3->value();
    macAddress[3] = ui->spinBox_mac_4->value();
    macAddress[4] = ui->spinBox_mac_5->value();
    macAddress[5] = ui->spinBox_mac_6->value();

    if(isStartTestCase){
        Message::showWarning(NULL, "警告", "请先停止老化测试，再进行设备设置！ ");
        return;
    }

    int devTypeIndex = ui->comboBox_devType->currentIndex();

    QString devTypeStr = "COME-B板 ";
    if(!(devTypeIndex == 0 || devTypeIndex == 2)){
        Message::showWarning(this, "警告", QString("只有COME-B板或者新版COME-B才有载板！").arg(netDevNums));
        return;
    }

    if(netDevNums != 2){
        Message::showWarning(this, "警告", QString("为检测到载板网卡，当前主板网卡数量为%1").arg(netDevNums));
        return;
    }
    QString infoStr = QString("确定设置MAC地址吗？，当前版本类型为%1 - 载板(请确保载板MAC与核心板MAC不一样)").arg(devTypeStr);
    if(!myHelper::ShowMessageBoxQuesion(infoStr)){
        SingleTonSToast::toast("操作已取消 ");
        return;
    }

    /* 修改一个网卡的MAC地址 */
    QString magicStr = "0x15338086";
    QString cmdStr = QString("ethtool -E enp%5s0 magic %4 offset 0x00 value %1;"
                             "ethtool -E enp%5s0 magic %4 offset 0x01 value %2;"
                             "ethtool -E enp%5s0 magic %4 offset 0x02 value %3;")
            .arg(macAddress[0])
            .arg(macAddress[1])
            .arg(macAddress[2])
            .arg(magicStr)
            .arg(7);

    emit startOneOrder_udp(devIndex, cmdStr, 10);

    cmdStr =         QString("ethtool -E enp%5s0 magic %4 offset 0x03 value %1;"
                             "ethtool -E enp%5s0 magic %4 offset 0x04 value %2;"
                             "ethtool -E enp%5s0 magic %4 offset 0x05 value %3;"
                             "ethtool -e enp%5s0 offset 0 length 6")
            .arg(macAddress[3])
            .arg(macAddress[4])
            .arg(macAddress[5])
            .arg(magicStr)
            .arg(7);
    emit startOneOrder_udp(devIndex, cmdStr, 10);
}
